package dhz.storage.configurations;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import dhz.storage.models.UploadTask;
import dhz.storage.models.UserUploadTasks;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String , UserUploadTasks> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String, UserUploadTasks> result = new RedisTemplate<>();
        result.setConnectionFactory(factory);

        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(UserUploadTasks.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);

        result.setKeySerializer(new StringRedisSerializer());
        result.setValueSerializer(serializer);
        result.setKeySerializer(new StringRedisSerializer());
        result.setHashValueSerializer(serializer);

        return result;
    }
}
